/**
* apisvr 定义
*/

#ifndef __APISVR_DEF_H__
#define __APISVR_DEF_H__

#include "CTBase.h"
#include "CTBuffer.h"
#include "CTStat.h"
#include "CTSizeString.h"
#include "CTArray.h"
#include "CTObjectMgr.h"
#include "CTHashObjectMgr.h"
#include "CTShmQueue.h"
#include "net/CTEpoll.h"
#include "CTMsgBase.h"
#include "CTConfig.h"
#include "CTServerUtils.h"
#include "CTAsyncCmd.h"
#include "CTAsyncCmdFactory.h"
#include "commmsg.h"

#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;
using namespace CTLib;

//===============================================
// 相关文件的定义，做为一个约定，不用在配置文件中读取

#define APPNAME             "apisvr"
#define FILE_CONFIG         "../config/" APPNAME ".conf"
#define FILE_NORMALLOG      "../log/" APPNAME ".log"
#define FILE_ERRORLOG       "../log/" APPNAME ".err"
#define FILE_STATLOG        "../log/" APPNAME ".sta"
#define FILE_PIDFILE        "../bin/" APPNAME ".pid"
#define FILE_UPDATELOG        "../log/" APPNAME ".upt"

#define CHECK_RETURN(iRet)  do { if (iRet) { return iRet; } } while(0)
#define CHECK_EXIT(iRet) do { if (iRet) { exit(iRet); } } while(0)
#define CHECK_THROW(iRet) do { if (iRet) { CT_THROW((iRet)); } } while(0)

//===============================================
// 这两个定义是为了能够在Windows下编译
#ifdef WIN32
    #define signal(signum, handler) (0)
    #define getopt(argc, argv, optstring) (0)

    #define SIGUSR1 10
    #define SIGUSR2 12
#else
    #include <sys/types.h>
    #include <signal.h>
#endif

//===============================================
enum api_const
{
    API_EVENT_SIZE =		64, ///< epoll参数
    API_WAIT_SIZE  =		64, ///< epoll参数
    MAX_IPLEN = 32,
};

//===============================================
// 统计项的定义

/**
* 异步命令的Do参数
*/
class CCmdDoParam
{
public:
    void*               m_pMain;
    CEnpNetHead*        m_pstHead;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdDoParam(void* pMain, CEnpNetHead& stHead, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
    m_pMain(pMain),
        m_pstHead(&stHead),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {   
        CT_ASSERT(m_pMain);
    }

private:
    // 禁止赋值
    CCmdDoParam& operator = (const CCmdDoParam& obj)
    {   
        return *this;    
    }
};

/**
* 异步命令的Answer参数
*/
class CCmdOnAnswerParam
{
public:
    void*               m_pMain;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdOnAnswerParam(void* pMain, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
    m_pMain(pMain),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {
        CT_ASSERT(m_pMain);
    }
private:
    // 禁止赋值
    CCmdOnAnswerParam& operator = (const CCmdOnAnswerParam& obj)
    {
        return *this;
    }
};

// 统计的CTLog
#define CT_STAT (CTLib::CTSingleton<CTLib::CTLog, 2>::Instance())

/**
* 统计项.
* 必须以0开始顺序编号, 方便和描述的下标对应
*/
enum EMainStat
{
    api_stat_recvmsg = 0,
    api_stat_sendmsg,
    api_stat_accept,
    api_stat_idleclose,
    api_stat_mainloop,
    api_stat_waittimeout,
    api_stat_check,
};

#endif //__APISVR_DEF_H__
